package com.dr.java.day05;

/**
 * Created by rui on 2017/12/30.
 */
public class ConstructorDemo {
    public static void main(String[] args) {
        Person1 p = new Person1(23, "deng");

        p.cry();
    }
}

//构造函数：在实例时调用，只调用一次
class Person1 {
    private int age;
    private String name;

//    构造代码块在构造函数前运行，对实例进行初始化
    {
        System.out.println("构造代码块运行");
        age = 2;
        name = "rui";
    }

//    构造函数是给特定的对象进行初始化
    public Person1(int age, String name) {
        System.out.println("构造函数运行");
        this.age = age;
        this.name = name;
    }

    public void cry() {
        System.out.println(name+": cry");
    }
}
